package com.koron.css2.clientCenter.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.mapper.QuickChangeUserMapper;
import com.koron.css2.clientCenter.queryBean.ChangeUserAcceptPrintQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.PrintInterfaceImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class ChangeUserAcceptPrint implements ServerInterface {

    private Logger log = LogManager.getLogger(ChangeUserAcceptPrint.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            ChangeUserAcceptPrintQueryBean queryBean = JsonUtils.objectToPojo(req.getData(), ChangeUserAcceptPrintQueryBean.class);
            if (ObjectUtil.isEmpty(queryBean.getPrintType())) {
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "请选择打印类型", null);
            }
            QuickChangeUserMapper mapper = factory.getMapper(QuickChangeUserMapper.class);
            PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
            ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>();
            queryBean.setAccount(userInfo.getUserInfo().getAcount());
            data = mapper.changeInfoPrintQuery(queryBean);
            /** 测试前端连续调用打印机是否正常，做通用处理 **/
            MessageBean<List> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
            List<String> msgList = new ArrayList<>();
            for (HashMap<String, String> map : data) {
                //设置打印数据
                ArrayList<HashMap<String, String>> printData = new ArrayList<HashMap<String,String>>();
                printData.add(map);
                String key = PrintInterfaceImpl.setPrintData(printData);
                msgList.add(key);
            }
//            //设置打印数据
//            String key = PrintInterfaceImpl.setPrintData(data);
            //返回key
            msg.setData(msgList);
            return msg;

        }catch (Exception e){
            logger.error("受理单打印失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "打印失败", null);
        }
    }
}
